<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JWT Structure Reference</title>
    <link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="../css/highlight.css" />
    <meta charset="utf-8">
    <script src="../js/jquery.min.js" defer></script>
    <script src="../js/jazzy.js" defer></script>
    
    <script src="../js/lunr.min.js" defer></script>
    <script src="../js/typeahead.jquery.js" defer></script>
    <script src="../js/jazzy.search.js" defer></script>
  </head>
  <body>

    <a name="//apple_ref/swift/Struct/JWT" class="dashAnchor"></a>

    <a title="JWT Structure Reference"></a>

    <header class="header">
      <p class="header-col header-col--primary">
        <a class="header-link" href="../index.html">
          SwiftJWT 3.6.200 Docs
        </a>
         (100% documented)
      </p>
    
      <p class="header-col--secondary">
        <form role="search" action="../search.json">
          <input type="text" placeholder="Search documentation" data-typeahead>
        </form>
      </p>
    
        <p class="header-col header-col--secondary">
          <a class="header-link" href="https://github.com/Kitura/Swift-JWT/">
            <img class="header-icon" src="../img/gh.png"/>
            View on GitHub
          </a>
        </p>
    
    </header>

    <p class="breadcrumbs">
      <a class="breadcrumb" href="../index.html">SwiftJWT Reference</a>
      <img class="carat" src="../img/carat.png" />
      JWT Structure Reference
    </p>

    <div class="content-wrapper">
      <nav class="navigation">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Classes.html">Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/ClaimsMicroProfile.html">ClaimsMicroProfile</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/ClaimsOpenID.html">ClaimsOpenID</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/ClaimsStandardJWT.html">ClaimsStandardJWT</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/JWTDecoder.html">JWTDecoder</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/JWTEncoder.html">JWTEncoder</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Protocols.html">Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Protocols/Claims.html">Claims</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Structs.html">Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/AddressClaim.html">AddressClaim</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/Header.html">Header</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWT.html">JWT</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWTError.html">JWTError</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWTSigner.html">JWTSigner</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/JWTVerifier.html">JWTVerifier</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/ValidateClaimsResult.html">ValidateClaimsResult</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">

        <section class="section">
          <div class="section-content top-matter">
            <h1>JWT</h1>
              <div class="declaration">
                <div class="language">
                  
                  <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">struct</span> <span class="kt">JWT</span><span class="o">&lt;</span><span class="kt">T</span><span class="o">&gt;</span> <span class="p">:</span> <span class="kt">Codable</span> <span class="k">where</span> <span class="kt">T</span> <span class="p">:</span> <span class="kt"><a href="../Protocols/Claims.html">Claims</a></span></code></pre>

                </div>
              </div>
            <p>A struct representing the <code><a href="../Structs/Header.html">Header</a></code> and <code><a href="../Protocols/Claims.html">Claims</a></code> of a JSON Web Token.</p>
<h3 id='usage-example' class='heading'>Usage Example:</h3>
<pre class="highlight swift"><code><span class="kd">struct</span> <span class="kt">MyClaims</span><span class="p">:</span> <span class="kt">Claims</span> <span class="p">{</span>
   <span class="k">var</span> <span class="nv">name</span><span class="p">:</span> <span class="kt">String</span>
<span class="p">}</span>
<span class="k">let</span> <span class="nv">jwt</span> <span class="o">=</span> <span class="kt">JWT</span><span class="p">(</span><span class="nv">claims</span><span class="p">:</span> <span class="kt">MyClaims</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"Kitura"</span><span class="p">))</span>
<span class="k">let</span> <span class="nv">key</span> <span class="o">=</span> <span class="s">"&lt;PrivateKey&gt;"</span><span class="o">.</span><span class="nf">data</span><span class="p">(</span><span class="nv">using</span><span class="p">:</span> <span class="o">.</span><span class="n">utf8</span><span class="p">)</span><span class="o">!</span>
<span class="k">let</span> <span class="nv">signedJWT</span><span class="p">:</span> <span class="kt">String</span><span class="p">?</span> <span class="o">=</span> <span class="k">try</span><span class="p">?</span> <span class="n">jwt</span><span class="o">.</span><span class="nf">sign</span><span class="p">(</span><span class="nv">using</span><span class="p">:</span> <span class="o">.</span><span class="nf">rs256</span><span class="p">(</span><span class="nv">key</span><span class="p">:</span> <span class="n">key</span><span class="p">,</span> <span class="nv">keyType</span><span class="p">:</span> <span class="o">.</span><span class="n">privateKey</span><span class="p">))</span>
</code></pre>

          </div>
        </section>

        <section class="section">
          <div class="section-content">
            <div class="task-group">
              <ul class="item-container">
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT0B0V6headerAA6HeaderVvp"></a>
                    <a name="//apple_ref/swift/Property/header" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT0B0V6headerAA6HeaderVvp">header</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>The JWT header.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="k">var</span> <span class="nv">header</span><span class="p">:</span> <span class="kt"><a href="../Structs/Header.html">Header</a></span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT0B0V6claimsxvp"></a>
                    <a name="//apple_ref/swift/Property/claims" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT0B0V6claimsxvp">claims</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>The JWT claims</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="k">var</span> <span class="nv">claims</span><span class="p">:</span> <span class="kt">T</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT0B0V6header6claimsACyxGAA6HeaderV_xtcfc"></a>
                    <a name="//apple_ref/swift/Method/init(header:claims:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT0B0V6header6claimsACyxGAA6HeaderV_xtcfc">init(header:<wbr>claims:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Initialize a <code>JWT</code> instance from a <code><a href="../Structs/Header.html">Header</a></code> and <code><a href="../Protocols/Claims.html">Claims</a></code>.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">header</span><span class="p">:</span> <span class="kt"><a href="../Structs/Header.html">Header</a></span> <span class="o">=</span> <span class="kt"><a href="../Structs/Header.html">Header</a></span><span class="p">(),</span> <span class="nv">claims</span><span class="p">:</span> <span class="kt">T</span><span class="p">)</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>header</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>A JSON Web Token header object.</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>claims</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>A JSON Web Token claims object.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>A new instance of <code>JWT</code>.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT0B0V9jwtString8verifierACyxGSS_AA11JWTVerifierVtKcfc"></a>
                    <a name="//apple_ref/swift/Method/init(jwtString:verifier:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT0B0V9jwtString8verifierACyxGSS_AA11JWTVerifierVtKcfc">init(jwtString:<wbr>verifier:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Initialize a <code>JWT</code> instance from a JWT String.
The signature will be verified using the provided JWTVerifier.
The time based standard JWT claims will be verified with <code>validateClaims()</code>.
If the string is not a valid JWT, or the verification fails, the initializer returns nil.</p>
<div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <code><a href="../Structs/JWTError.html#/s:8SwiftJWT8JWTErrorV16invalidJWTStringACvpZ">JWTError.invalidJWTString</a></code> if the provided String is not in the form mandated by the JWT specification.

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <code><a href="../Structs/JWTError.html#/s:8SwiftJWT8JWTErrorV18failedVerificationACvpZ">JWTError.failedVerification</a></code> if the verifier fails to verify the jwtString.

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    A DecodingError if the JSONDecoder throws an error while decoding the JWT.

</div>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">jwtString</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">verifier</span><span class="p">:</span> <span class="kt"><a href="../Structs/JWTVerifier.html">JWTVerifier</a></span> <span class="o">=</span> <span class="o">.</span><span class="k">none</span><span class="p">)</span> <span class="k">throws</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>jwt</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>A String with the encoded and signed JWT.</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>verifier</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code><a href="../Structs/JWTVerifier.html">JWTVerifier</a></code> used to verify the JWT.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>An instance of <code>JWT</code> if the decoding succeeds.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT0B0V4sign5usingSSAA9JWTSignerV_tKF"></a>
                    <a name="//apple_ref/swift/Method/sign(using:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT0B0V4sign5usingSSAA9JWTSignerV_tKF">sign(using:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Sign the JWT using the given algorithm and encode the header, claims and signature as a JWT String.</p>
<div class="aside aside-note">
    <p class="aside-title">Note</p>
    <p>This function will set header.alg field to the name of the signing algorithm.</p>

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <p>An EncodingError if the JSONEncoder throws an error while encoding the JWT.</p>

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <p><code><a href="../Structs/JWTError.html#/s:8SwiftJWT8JWTErrorV14osVersionToLowACvpZ">JWTError.osVersionToLow</a></code> if not using macOS 10.12.0 (Sierra) or iOS 10.0 or higher.</p>

</div><div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    <p>A Signing error if the jwtSigner is unable to sign the JWT with the provided key.</p>

</div>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="k">mutating</span> <span class="kd">func</span> <span class="nf">sign</span><span class="p">(</span><span class="n">using</span> <span class="nv">jwtSigner</span><span class="p">:</span> <span class="kt"><a href="../Structs/JWTSigner.html">JWTSigner</a></span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">String</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>A String with the encoded and signed JWT.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT0B0V6verify_5usingSbSS_AA11JWTVerifierVtFZ"></a>
                    <a name="//apple_ref/swift/Method/verify(_:using:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT0B0V6verify_5usingSbSS_AA11JWTVerifierVtFZ">verify(_:<wbr>using:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Verify the signature of the encoded JWT using the given algorithm.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">static</span> <span class="kd">func</span> <span class="nf">verify</span><span class="p">(</span><span class="n">_</span> <span class="nv">jwt</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="n">using</span> <span class="nv">jwtVerifier</span><span class="p">:</span> <span class="kt"><a href="../Structs/JWTVerifier.html">JWTVerifier</a></span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>jwt</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>A String with the encoded and signed JWT.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>A Bool indicating whether the verification was successful.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:8SwiftJWT0B0V14validateClaims6leewayAA08ValidateD6ResultVSd_tF"></a>
                    <a name="//apple_ref/swift/Method/validateClaims(leeway:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:8SwiftJWT0B0V14validateClaims6leewayAA08ValidateD6ResultVSd_tF">validateClaims(leeway:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Validate the time based standard JWT claims.
This function checks that the &ldquo;exp&rdquo; (expiration time) is in the future
and the &ldquo;iat&rdquo; (issued at) and &ldquo;nbf&rdquo; (not before) headers are in the past,</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">validateClaims</span><span class="p">(</span><span class="nv">leeway</span><span class="p">:</span> <span class="kt">TimeInterval</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt"><a href="../Structs/ValidateClaimsResult.html">ValidateClaimsResult</a></span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>leeway</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The time in seconds that the JWT can be invalid but still accepted to account for clock differences.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>A value of <code><a href="../Structs/ValidateClaimsResult.html">ValidateClaimsResult</a></code>.</p>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </section>

      </article>
    </div>
    <section class="footer">
      <p>&copy; 2020 <a class="link" href="https://github.com/Kitura/Swift-JWT" target="_blank" rel="external">IBM &amp; Kitura Project Contributors</a>. All rights reserved. (Last updated: 2020-11-25)</p>
      <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
    </section>
  </body>
</div>
</html>
